package net.wanho.shop.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import net.wanho.shop.mapper.MemberMapper;
import net.wanho.shop.po.Member;
import net.wanho.shop.service.MemberService;
import net.wanho.shop.vo.PageVo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 猛虎王
 * @since 2024-12-24
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class MemberServiceImpl implements MemberService {

    private final MemberMapper memberMapper;

    /**
     * 会员表分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param member
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo findPage(int pageNum, int pageSize, Member member) {
        if (ObjectUtil.isNull(member)) {
            member = new Member();
        }

        // 分页
        PageHelper.startPage(pageNum, pageSize);
        List<Member> memberList = memberMapper.select(member);
        return new PageInfo(memberList);
    }


    /**
     * 会员表分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param member
     * @return
     */
    @Override
    public PageVo findPageByMember(int pageNum, int pageSize, Member member) {
        if (ObjectUtil.isNull(member)) {
            member = new Member();
        }

        PageHelper.startPage(pageNum, pageSize);
        List<Member> members = memberMapper.select(member);
        PageInfo<Member> memberList = new PageInfo<>(members);

        PageVo pageVo = new PageVo(memberList.getTotal(), pageNum);
        pageVo.put("members", memberList.getList());

        return pageVo;
    }

    /**
     * 根据主键修改会员表
     *
     * @param member
     * @param id
     */
    @Override
    public void modifyById(Member member, Integer id) {
        // 根据传过来的从数据库获取会员信息
        Member member1 = memberMapper.selectById(id);
        // 判断是否存在
        if (!ObjectUtil.isNotNull(member1))
            throw new RuntimeException("会员不存在");

        member1.setUsername(member.getUsername()); // 用户
        member1.setEmail(member.getEmail()); // 邮箱
        member1.setGender(member.getGender()); // 性别
        member1.setQq(member.getQq()); // QQ
        member1.setPhone(member.getPhone()); // 手机
        member1.setEducation(member.getEducation()); // 学历
        member1.setHobby(member.getHobby()); // 爱好
        member1.setIntroduce(member.getIntroduce()); // 简介

        memberMapper.update(member1);

    }

    @Override
    public void removeByIdList(ArrayList<Integer> ids) {
//        List<Integer> list = Arrays.stream(ids).toList();
//        memberMapper.deleteByIds(Arrays.stream(ids));
        memberMapper.deleteByIds(ids);

        System.out.println(ids + "-----------");
    }

    /**
     * 根据主键查询会员表
     *
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Member findById(Serializable id) {
        return memberMapper.selectById(id);
    }

    /**
     * 会员表新增
     *
     * @param member
     */
    @Override
    public void add(Member member) {
        // 密码加密
        member.setPassword(SecureUtil.md5(member.getPassword()));
        // 创建时间
        member.setCreateTime(LocalDateTime.now());

        memberMapper.insert(member);
    }

    /**
     * 修改会员表
     *
     * @param member
     */
    @Override
    public void modify(Member member) {
        memberMapper.update(member);
    }

    /**
     * 根据主键删除会员表
     *
     * @param id
     */
    @Override
    public void removeById(Serializable id) {
        if (((String) id).contains(",")) {
            memberMapper.deleteByIds(((String) id).split(","));
        } else {
            memberMapper.deleteById(id);
        }
    }

    @Override
    public Workbook export(int pageNum, int pageSize, Member member) {
        PageHelper.startPage(pageNum, pageSize);
        List<Member> list = memberMapper.select(member);

        ExportParams params = new ExportParams("会员信息表", "会员表");
        Workbook workbook = ExcelExportUtil.exportExcel(params, Member.class, list);


        return workbook;
    }


    @SneakyThrows
    @Override
    public void importExcel(MultipartFile multipartFile) {

        ImportParams params = new ImportParams();

        params.setTitleRows(1);
        params.setHeadRows(1);

        List<Member> list = ExcelImportUtil.importExcel(multipartFile.getInputStream(), Member.class, params);
        // 处理
        list.forEach(member -> {
            member.setPassword(SecureUtil.md5("123"));
            member.setCreateTime(LocalDateTime.now());
            member.setUpdateTime(LocalDateTime.now());
            member.setIsDeleted(0);
        });
        memberMapper.insertBatch(list);
    }


}
